<?php
/**
 * 个人资料
 * 
 */
 
include_once(GZ_PATH . '/includes/personal_func.php');
class personalAction extends Action{
    /**我的-个人资料
     */
    public function personal_info(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $user = get_userInfo($uid);
        GZ_Api::outPut($user);
    }
//修改个人资料
    public function edit_userinfo(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $data["headimg"] = _POST("headimg","");
        $data["nickname"] = _POST("nickname","");
        $data["sex"] = _POST("sex","");
        $data["age"] = _POST("age","");
        $data["height"] = _POST("height","");
        $data["weight"] = _POST("weight",""); 
        $data["blood_type"] = _POST("blood_type","");
        $data["feature_desc"] = _POST("feature_desc","");
        $data["adverse_effect"] = _POST("adverse_effect","");
        $sql = "SELECT `headimg` FROM ".$this->table("users")." WHERE `user_id`=".$uid;
        $old_headimg = $this->getOne($sql);
        $res = $this->autoExecute($this->table('users'), $data, 'UPDATE', "user_id = ".$uid);
        if($res){
            if($old_headimg != $data["headimg"]){
                @unlink($_SERVER['DOCUMENT_ROOT'].$old_headimg);
            }
            GZ_API::message("编辑成功",1);
        }else{
            GZ_API::message("编辑失败",2);
        }
    }

    //我的求助
    public function seek_help(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $page_size   = GZ_Api::$pagination['count'];    //分页设置
        $page        = GZ_Api::$pagination['page'];     //分页设置
        $res=get_help($uid,$page,$page_size);
        if($res){
          GZ_Api::outPut($res);
        }else{
          GZ_Api::outPut(13);
        }
       
    }
    //我的救助
    public function my_rescue(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $page_size   = GZ_Api::$pagination['count'];    //分页设置
        $page        = GZ_Api::$pagination['page'];     //分页设置
        $res=get_rescue($uid,$page,$page_size);
        if($res){
          GZ_Api::outPut($res);
        }else{
          GZ_Api::outPut(13);
        }
    }
    //我的钱包
     public function my_wallet(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $data=wallet_account($uid);
        GZ_Api::outPut($data);
     }
    //收支明细
    public function account_log(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $type=1;//金额
        $page_size   = GZ_Api::$pagination['count'];    //分页设置
        $page        = GZ_Api::$pagination['page'];     //分页设置  
        $res = get_account($uid,$type,$page,$page_size);
        if($res){
          GZ_Api::outPut($res);
        }else{
          GZ_Api::outPut(13);
        }
       
     } 
   //我的积分
    public function integral_log(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $type=2;//积分
        $page_size   = GZ_Api::$pagination['count'];    //分页设置
        $page        = GZ_Api::$pagination['page'];     //分页设置  
        $res = get_account($uid,$type,$page,$page_size);
        if($res){
          GZ_Api::outPut($res);
        }else{
          GZ_Api::outPut(13);
        }
       
     } 
  //我的套餐
    public function package(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $data=array();
        $data['mycard']=get_mycard($uid);//我的套餐
        $data['card_list']=get_card();//会员套餐列表
        GZ_Api::outPut($data);
    }

    public function card_info(){
        $card_id=_POST('card_id',0);
        $card_info = get_cardInfo($card_id);
        GZ_Api::outPut($card_info);
    } 

    //套餐详情
    public function card_detail(){
        $card_id=_POST('card_id',0);
        $data['card_info_h5']='http://47.104.65.183:8082/Mapi/public/html/card_detail.php?card_id='.$card_id;
        GZ_Api::outPut($data);
    }

    //提交会员认证 （普通用户）
    public function user_auth(){
        GZ_Api::authToken();
		
        $user_id	 = _POST("user_id",0);
        $auth_name	 = _POST('user_name','');//真实姓名
        $identity_card=_POST('identity_card','');//身份证号
        $face_card	 = _POST('img_url1','');//身份证正面
        $back_card	 = _POST('img_url2','');//身份证反面
		
		if(empty($auth_name)){GZ_API::message("真实姓名不能为空",2);}
        if(empty($identity_card)){GZ_API::message("身份证号码不能为空",2);}
		if(empty($face_card)){GZ_API::message("请选择一张身份证正面照片",2);}
        if(empty($back_card)){GZ_API::message("请选择一张身份证反面照片",2);}
		
        $auth_info=$this->getRow('select * from '.$this->table('user_auth').' where user_id='.$user_id);
		
        if($auth_info){
			//=========已经提交过的用户========================
            if($auth_info['auth_status'] == 0 ){     
              GZ_API::message("认证申请审核中,请等待验证",2);
            }else if ($auth_info['auth_status'] == 1 ) {
              GZ_API::message("您的认证申请已通过！",2);
            }else if ($auth_info['auth_status'] == 2 ){
				//重新审核 --审核不通过的用户
				$data['auth_name']	 = $auth_name;
				$data['identity_card'] = $identity_card;
				$data['face_card']	 = $face_card;
				$data['back_card']	 = $back_card;
				$data['auth_status'] = 0; //未审核 （重新审核）
				$data['add_time']	 = time();
			 	$res = $this->autoExecute($this->table('user_auth'), $data, 'UPDATE', "auth_id = ".$auth_info['auth_id']);
				
				if($res){
				  GZ_Api::message("重新审核已提交，请等待客服审核",1);
				}else{
				  GZ_Api::message("提交失败",2);
				}
			}
			
        }else{
			//==============未提交的用户=======================
			$data['auth_type']	 = 1;
			$data['user_id']	 = $user_id;
			$data['auth_name']	 = $auth_name;
			$data['identity_card'] = $identity_card;
			$data['face_card']	 = $face_card;
			$data['back_card']	 = $back_card;
			$data['auth_status'] = 0; //未审核 （重新审核）
			$data['add_time']	 = time();
		 	$res = $GLOBALS["db"]->autoExecute($GLOBALS["db"]->table("user_auth"),$data,"INSERT");
			
			if($res){
			  GZ_Api::message("审核已提交，请等待客服审核",1);
			}else{
			  GZ_Api::message("提交失败",2);
			}
		}

    }
      //医师认证
    public function doctor_auth(){
      GZ_Api::authToken();
      $data=array();
      $type=_POST('action_type','');
      $data['user_id'] = _POST("user_id",0);
      $data['auth_name']=_POST('user_name','');//真实姓名
      $data['identity_card']=_POST('identity_card','');//身份证号
      $data['auth_project']=_POST('project','');//主治项目
      $data['auth_hospital']=_POST('hospital','');//所在医院
      if(empty($data['auth_name'])){GZ_API::message("真实姓名不能为空",2);}
      if(empty($data['identity_card'])){GZ_API::message("身份证号码不能为空",2);}
      if(empty($data['auth_project'])){GZ_API::message("真实姓名不能为空",2);}
      if(empty($data['auth_hospital'])){GZ_API::message("身份证号码不能为空",2);}
      $data['face_card']=_POST('img_url1','');//身份证正面
      $data['back_card']=_POST('img_url2','');//身份证反面
      $data['auth_img']=_POST('img_url3','');//医师执照
      $data['auth_type']=2;
      $data['auth_status']=0;
      $data['add_time']=time();
      $auth_info=$this->getRow('select * from '.$this->table('user_auth').' where user_id='.$data['user_id']);
      if($auth_info){
        if($auth_info['auth_status'] == 0 ){
        GZ_API::message("该申请已提交过,请等待验证",2);
        }else if ($auth_info['auth_status'] == 1 ) {
          GZ_API::message("该申请已通过！！",2);
        }
      }
      
      if($type=='add'){
        $res = $GLOBALS["db"]->autoExecute($GLOBALS["db"]->table("user_auth"),$data,"INSERT");
      }else{
        $res = $this->autoExecute($this->table('user_auth'), $data, 'UPDATE', "user_id = ".$data['user_id']);
      }


      if($res){
        GZ_Api::message("提交成功",1);
      }else{
        GZ_Api::message("提交失败",2);
      }
}

//获取验证状态
	public function get_auth()
	{
		GZ_Api::authToken();
		$user_id = _POST("user_id",0);

		$auth_info = $this->getRow('select * from '.$this->table('user_auth').' where user_id='.$user_id);
	  
		$data = array();
		if(!empty($auth_info)){
	
			$data['auth_name'] = $auth_info['auth_name'];
			$data['identity_card'] = $auth_info['identity_card'];
			$data['auth_remark'] = !empty($auth_info['remark'])?$auth_info['remark']:'';
			if($auth_info['auth_status']==0){//未审核
				$data['is_validated']=1;
				$data['status_explain']='你的实名认证审核中';
			}else if($auth_info['auth_status']==1){
				$data['is_validated']=2;
				$data['status_explain']='实名认证审核通过';
			}else if($auth_info['auth_status']==2){
				$data['is_validated']=3;
				$data['status_explain']='审核不通过';
			}
			
		}else{
			$data['auth_name'] = '';
			$data['identity_card'] = '';
			$data['auth_remark'] = '';
			$data['is_validated'] = 0;//未提交状态
			$data['status_explain'] = '实名认证未提交';
		}
     
      	GZ_Api::outPut($data);
   }

//重新验证
   public function update_auth(){
        GZ_Api::authToken();
        $user_id = _POST("user_id",0);
        $type=_POST('type','');//1：普通用户 2：医师
        $data=array();
        $info=$this->getRow('select * from '.$this->table('user_auth').' where user_id='.$user_id.' and auth_status=2');
        if($info){
          $data['user_id']=$info['user_id'];
          $data['auth_name']=$info['auth_name'];
          $data['identity_card']=$info['identity_card'];//身份证号
          $data['auth_project']=$info['auth_project'];
          $data['auth_hospital']=$info['auth_hospital'];
          $data['face_card']=$info['face_card'];
          $data['back_card']=$info['back_card'];
          $data['auth_img']=$info['auth_img'];
        }
        GZ_Api::outPut($data);

   }
   //标签首页
   public function label_list(){
        GZ_Api::authToken();
        $user_id =_POST("user_id");
        $sql='select label from '.$this->table('users').' where user_id='.$user_id;
        $res=$GLOBALS['db']->getRow($sql);
        $label=json_decode($res['label'],true);

        $data=array();
        if(!empty($label)){
            foreach ($label as $key => $value) {  
             $row['label_index']= $key;
             $row['label_name']= $value;
             $data[] = $row;
            }
        }


        GZ_Api::outPut($data);
   }
   //添加修改标签
   public function edit_label(){
        GZ_Api::authToken();
        $user_id=_POST('user_id','');
        $type=_POST('type','');//add:添加 edit：修改
        $label_len=str_len(_POST('label',''));//标签
        if($label_len>8){ 
          GZ_API::message("标签不能超过6个字",2); 
        }
        $label=_POST('label','');
        $user=$this->getOne('select label from '.$this->table('users').' where user_id='.$user_id); 
     //添加标签
        if($type=='add'){      
            if(!empty($user)){
              $old_label=json_decode($user,true);
              if(count($old_label)>=3){
                GZ_API::message("不能超过3个标签",2);
              }
              array_push($old_label,$label);
          }else{
              $old_label=array(
              $label,
              );
          }  
          $data['label']=json_encode($old_label,JSON_UNESCAPED_UNICODE);
        }else{
        //修改标签
          $label_index=_POST('label_index',0);//指定的下标
          $old_label=json_decode($user,true);
          foreach ($old_label as $k => $v) {
            $old_label[$label_index]=$label;
          } 
          $data['label']=json_encode($old_label,JSON_UNESCAPED_UNICODE);     
        }

        $res = $this->autoExecute($this->table('users'), $data, 'UPDATE', "user_id = ".$user_id);
        if($res){
          GZ_Api::message("编辑成功",1);
        }else{
          GZ_Api::message("编辑失败",2);
        }
   }



}

?>